package com.ulife.item.module.screen;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.ulife.common.WebConstant;

public class Itempic {
	private static final Logger log = LoggerFactory.getLogger(Itempic.class);

	@Autowired
	private HttpServletRequest request;

	@Autowired
	private HttpServletResponse response;

	public void execute() {
		InputStream inputStream = null;
		OutputStream outputStream = null;
		try {
			String path = request.getParameter("p");
			inputStream = new FileInputStream(new File(WebConstant.FILE_UPLOAD_PATH + path));
			int i = inputStream.available();
			byte data[] = new byte[i];
			inputStream.read(data);
			inputStream.close();
			response.setContentType("image/*");
			outputStream = response.getOutputStream();
			outputStream.write(data);
			outputStream.close();
		} catch (Exception e) {// 错误处理
			try {
				inputStream = new FileInputStream(new File(WebConstant.FILE_UPLOAD_PATH + WebConstant.ITEM_PIC_FOLDER + File.separator
						+ "default.png"));
				int i = inputStream.available();
				byte data[] = new byte[i];
				inputStream.read(data);
				inputStream.close();
				response.setContentType("image/*");
				outputStream = response.getOutputStream();
				outputStream.write(data);
				outputStream.close();
			} catch (Exception e1) {
				log.error("默认文件异常 ", e1);
			}
		} finally {
			if (inputStream != null) {
				try {
					inputStream.close();
				} catch (IOException e) {
					log.error("关闭异常  ", e);
				}
			}
			if (outputStream != null) {
				try {
					outputStream.close();
				} catch (IOException e) {
					log.error("关闭异常  ", e);
				}
			}
		}
	}
}
